package o;

import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.util.Log;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class fib {
    public <T> T ॱ(Bundle bundle, Type type) {
        T t = null;
        if (!(bundle == null || type == null)) {
            Class ˎ = fip.ˎ(type);
            if (ˎ == List.class) {
                try {
                    t = ˋ(type, bundle);
                } catch (Exception e) {
                }
            } else {
                try {
                    t = ˏ(bundle, ˎ.newInstance());
                } catch (Exception e2) {
                }
            }
        }
        return t;
    }

    public <T> T ˏ(Bundle bundle, T t) {
        return (bundle == null || t == null) ? t : ˋ(bundle, (Object) t, t.getClass());
    }

    private <T> T ˋ(Bundle bundle, T t, Type type) {
        Throwable e;
        if (!(bundle == null || t == null)) {
            Class cls = t.getClass();
            while (cls != null && cls != Object.class) {
                for (Field field : cls.getDeclaredFields()) {
                    try {
                        ˏ(type, t, field, bundle);
                    } catch (IllegalAccessException e2) {
                        e = e2;
                    } catch (IllegalArgumentException e3) {
                        e = e3;
                    }
                }
                type = fip.ˎ(type, cls, cls.getGenericSuperclass());
                cls = cls.getSuperclass();
            }
        }
        return t;
        Log.e("MessageCodec", "decode, set value of the field exception, field name:" + field.getName(), e);
    }

    private void ˏ(Type type, Object obj, Field field, Bundle bundle) throws IllegalAccessException {
        if (!Modifier.isTransient(field.getModifiers())) {
            Object ˏ = ˏ(type, field, bundle);
            if (ˏ != null) {
                boolean isAccessible = field.isAccessible();
                field.setAccessible(true);
                field.set(obj, ˏ);
                field.setAccessible(isAccessible);
            }
        }
    }

    private Object ˏ(Type type, Field field, Bundle bundle) {
        String name = field.getName();
        Object obj = bundle.get(name);
        if (!(obj instanceof Bundle) || field.getDeclaringClass() == fic.class) {
            return obj;
        }
        try {
            Bundle bundle2 = (Bundle) obj;
            int i = bundle2.getInt("_val_type_", -1);
            if (i == 1) {
                return ˋ(fip.ˎ(type, field.getDeclaringClass(), field.getGenericType()), bundle2);
            }
            if (i != 0) {
                return obj;
            }
            Type ˎ = fip.ˎ(type, field.getDeclaringClass(), field.getGenericType());
            return ˋ((Bundle) obj, fip.ˎ(ˎ).newInstance(), ˎ);
        } catch (Throwable e) {
            Log.e("MessageCodec", "decode, read value of the field exception, field name: " + name, e);
            return null;
        }
    }

    protected List<Object> ˋ(Type type, Bundle bundle) throws InstantiationException, IllegalAccessException {
        int i = bundle.getInt("_list_size_");
        List<Object> arrayList = new ArrayList(i);
        for (int i2 = 0; i2 < i; i2++) {
            Object obj = bundle.get("_list_item_" + i2);
            if (obj.getClass().isPrimitive() || (obj instanceof String) || (obj instanceof Serializable)) {
                arrayList.add(obj);
            } else if (obj instanceof Bundle) {
                Bundle bundle2 = (Bundle) obj;
                int i3 = bundle2.getInt("_val_type_", -1);
                if (i3 == 1) {
                    throw new InstantiationException("Nested List can not be supported");
                } else if (i3 == 0) {
                    arrayList.add(ˏ(bundle2, ((Class) ((ParameterizedType) type).getActualTypeArguments()[0]).newInstance()));
                } else {
                    throw new InstantiationException("Unknown type can not be supported");
                }
            } else {
                continue;
            }
        }
        return arrayList;
    }

    public Bundle ˋ(Object obj, Bundle bundle) {
        Throwable e;
        Class cls = obj.getClass();
        while (cls != null && cls != Object.class) {
            for (Field field : cls.getDeclaredFields()) {
                try {
                    ˋ(obj, field, bundle);
                } catch (IllegalAccessException e2) {
                    e = e2;
                } catch (IllegalArgumentException e3) {
                    e = e3;
                }
            }
            cls = cls.getSuperclass();
        }
        return bundle;
        Log.e("MessageCodec", "encode, get value of the field exception, field name: " + field.getName(), e);
    }

    private void ˋ(Object obj, Field field, Bundle bundle) throws IllegalAccessException {
        if (!Modifier.isTransient(field.getModifiers())) {
            boolean isAccessible = field.isAccessible();
            field.setAccessible(true);
            ˋ(field.getName(), field.get(obj), bundle);
            field.setAccessible(isAccessible);
        }
    }

    private void ˋ(String str, Object obj, Bundle bundle) {
        if (obj != null) {
            if (obj instanceof String) {
                bundle.putString(str, (String) obj);
            } else if (obj instanceof Integer) {
                bundle.putInt(str, ((Integer) obj).intValue());
            } else if (obj instanceof Short) {
                bundle.putShort(str, ((Short) obj).shortValue());
            } else if (obj instanceof Long) {
                bundle.putLong(str, ((Long) obj).longValue());
            } else if (obj instanceof Float) {
                bundle.putFloat(str, ((Float) obj).floatValue());
            } else if (obj instanceof Double) {
                bundle.putDouble(str, ((Double) obj).doubleValue());
            } else if (obj instanceof Boolean) {
                bundle.putBoolean(str, ((Boolean) obj).booleanValue());
            } else if (obj instanceof CharSequence) {
                bundle.putCharSequence(str, (CharSequence) obj);
            } else if (obj instanceof IBinder) {
                fhx.ॱ(bundle, str, (IBinder) obj);
            } else if (obj instanceof Parcelable) {
                bundle.putParcelable(str, (Parcelable) obj);
            } else if (obj instanceof byte[]) {
                bundle.putByteArray(str, (byte[]) obj);
            } else if (obj instanceof List) {
                ॱ(str, (List) obj, bundle);
            } else if (obj instanceof Serializable) {
                bundle.putSerializable(str, (Serializable) obj);
            } else if (obj.getClass() != Object.class) {
                Bundle ˋ = ˋ(obj, new Bundle());
                ˋ.putInt("_val_type_", 0);
                bundle.putBundle(str, ˋ);
            }
        }
    }

    protected void ॱ(String str, List list, Bundle bundle) {
        Bundle bundle2 = new Bundle();
        bundle2.putInt("_val_type_", 1);
        bundle2.putInt("_list_size_", list.size());
        for (int i = 0; i < list.size(); i++) {
            ˋ("_list_item_" + i, list.get(i), bundle2);
        }
        bundle.putBundle(str, bundle2);
    }
}
